﻿using CoffeeShop.DAO;
using CoffeeShop.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CoffeeShop.BIZ
{
    public class ProductBiz
    {
        private ProductDao _productDao = new ProductDao();

        /// <summary>
        /// Check is valid product information
        /// </summary>
        /// <param name="categoryName"></param>
        /// <param name="unitId"></param>
        /// <param name="price"></param>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public bool IsValidProductModel(string categoryName, string unitId, string price, string categoryId)
        {
            if (string.IsNullOrEmpty(categoryName))
            {
                return false;
            }
            if (string.IsNullOrEmpty(unitId))
            {
                return false;
            }
            if (string.IsNullOrEmpty(price))
            {
                return false;
            }
            if (string.IsNullOrEmpty(categoryId))
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// Convert value to Product object
        /// </summary>
        /// <param name="productName">Name</param>
        /// <param name="unitId">UnitId</param>
        /// <param name="price">Price</param>
        /// <param name="categoryId">CategoryId</param>
        /// <param name="productId">ID</param>
        /// <param name="isDeleted">IsDeleted</param>
        /// <returns>Product object</returns>
        public Product ConvertToProductObject(string productName, int unitId, double price, int categoryId, string imageUrl = "", int productId = 0, bool isDeleted = false)
        {
            Product product = new Product();
            product.ID = productId;
            product.Name = productName;
            product.UnitId = unitId;
            product.Price = price;
            product.CategoryId = categoryId;
            product.IsDeleted = isDeleted;
            product.ImageURL = imageUrl;
            return product;
        }

        /// <summary>
        /// Get all products in DB
        /// </summary>
        /// <returns>List of product</returns>
        public List<Product> GetListProducts()
        {
            return _productDao.GetListProducts();
        }

        /// <summary>
        /// Get a category by CategoryId
        /// </summary>
        /// <param name="categoryId">CategoryId</param>
        /// <returns>True: Successful --- False: Fail</returns>
        public Product GetProductById(int productId)
        {
            return _productDao.GetProductById(productId);
        }

        /// <summary>
        /// Create a Product
        /// </summary>
        /// <param name="category">Product object</param>
        /// <returns>True: Successful --- False: Fail</returns>
        public bool CreateProduct(Product product)
        {
            return _productDao.CreateProduct(product);
        }

        /// <summary>
        /// Update a Product
        /// </summary>
        /// <param name="areaId">Product object</param>
        /// <returns>True: Successful --- False: Fail</returns>
        public bool UpdateProduct(Product product)
        {
            return _productDao.UpdateProduct(product);
        }

        /// <summary>
        /// Delete a Product
        /// </summary>
        /// <param name="categoryId">productId</param>
        /// <returns>True: Successful --- False: Fail</returns>
        public bool DeleteProduct(int productId)
        {
            return _productDao.DeleteProduct(productId);
        }

        /// <summary>
        /// Check is existed a product when update a product
        /// </summary>
        /// <param name="categoryId">Id</param>
        /// <param name="categoryName">Name</param>
        /// <returns>True: Existed -- False: Not existed</returns>
        public bool IsExistedProductCreate(string productName, int categoryId)
        {
            return _productDao.IsExistedProductCreate(productName, categoryId);
        }

        /// <summary>
        /// Check is existed a product when update a product
        /// </summary>
        /// <param name="categoryId">Id</param>
        /// <param name="categoryName">Name</param>
        /// <returns>True: Existed -- False: Not existed</returns>
        public bool IsExistedProductUpdate(int productId, string productName, int categoryId)
        {
            return _productDao.IsExistedProductUpdate(productId, productName, categoryId);
        }
    }
}